{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pddl.logic import Predicate, constants, variables\n",
    "from pddl.core import Domain, Problem, Action, Requirements\n",
    "from pddl.formatter import domain_to_string, problem_to_string\n",
    "from pddl import parse_domain, parse_problem"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('../../..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "problem_path = \"LLMs-Planning/llm_planning_analysis/instances/blocksworld/generated_basic/instance-131.pddl\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "domain_path = \"examples/rap_blocksworld/data/generated_domain.pddl\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "plan_path = \"examples/rap_blocksworld/data/sas_plan\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "val_path = os.getenv(\"VAL\")\n",
    "cmd = f\"{val_path}/validate -v {domain_path} {problem_path} {plan_path}\"\n",
    "response = os.popen(cmd).read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "change_str = response.split(\"-----------------------\")[-1].split(\"Plan executed successfully\")[0].strip().split(\"\\n\\n\")\n",
    "changes = []\n",
    "for c in change_str:\n",
    "    changes.append(c.split(\"\\n\")[1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "problem = parse_problem(problem_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "frozenset({Predicate(clear, a),\n",
       "           Predicate(handempty, ),\n",
       "           Predicate(on, a, c),\n",
       "           Predicate(on, c, d),\n",
       "           Predicate(on, d, b),\n",
       "           Predicate(ontable, b)})"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "problem.init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "And([Predicate(on, b, c), Predicate(on, c, d)])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "problem.goal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(changes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['(on a c)', '(clear a)', '(handempty)']\n",
      "['(holding a)', '(clear c)']\n",
      "{Predicate(holding, a), Predicate(on, c, d), Predicate(ontable, b), Predicate(on, d, b), Predicate(clear, c)}\n",
      "['(holding a)']\n",
      "['(clear a)', '(handempty)', '(ontable a)']\n",
      "{Predicate(handempty, ), Predicate(on, c, d), Predicate(ontable, a), Predicate(ontable, b), Predicate(on, d, b), Predicate(clear, c), Predicate(clear, a)}\n",
      "['(on c d)', '(clear c)', '(handempty)']\n",
      "['(holding c)', '(clear d)']\n",
      "{Predicate(clear, d), Predicate(holding, c), Predicate(ontable, a), Predicate(ontable, b), Predicate(on, d, b), Predicate(clear, a)}\n",
      "['(clear a)', '(holding c)']\n",
      "['(handempty)', '(clear c)', '(on c a)']\n",
      "{Predicate(clear, d), Predicate(handempty, ), Predicate(on, c, a), Predicate(ontable, a), Predicate(ontable, b), Predicate(on, d, b), Predicate(clear, c)}\n",
      "['(on d b)', '(clear d)', '(handempty)']\n",
      "['(holding d)', '(clear b)']\n",
      "{Predicate(clear, b), Predicate(on, c, a), Predicate(ontable, a), Predicate(ontable, b), Predicate(clear, c), Predicate(holding, d)}\n",
      "['(holding d)']\n",
      "['(clear d)', '(handempty)', '(ontable d)']\n",
      "{Predicate(clear, b), Predicate(ontable, d), Predicate(on, c, a), Predicate(clear, d), Predicate(handempty, ), Predicate(ontable, a), Predicate(ontable, b), Predicate(clear, c)}\n",
      "['(on c a)', '(clear c)', '(handempty)']\n",
      "['(holding c)', '(clear a)']\n",
      "{Predicate(clear, b), Predicate(ontable, d), Predicate(clear, d), Predicate(holding, c), Predicate(ontable, a), Predicate(ontable, b), Predicate(clear, a)}\n",
      "['(clear d)', '(holding c)']\n",
      "['(handempty)', '(clear c)', '(on c d)']\n",
      "{Predicate(clear, b), Predicate(ontable, d), Predicate(handempty, ), Predicate(on, c, d), Predicate(ontable, a), Predicate(ontable, b), Predicate(clear, c), Predicate(clear, a)}\n",
      "['(clear b)', '(ontable b)', '(handempty)']\n",
      "['(holding b)']\n",
      "{Predicate(ontable, d), Predicate(on, c, d), Predicate(ontable, a), Predicate(clear, c), Predicate(holding, b), Predicate(clear, a)}\n",
      "['(clear c)', '(holding b)']\n",
      "['(handempty)', '(clear b)', '(on b c)']\n",
      "{Predicate(clear, b), Predicate(ontable, d), Predicate(on, b, c), Predicate(handempty, ), Predicate(on, c, d), Predicate(ontable, a), Predicate(clear, a)}\n"
     ]
    }
   ],
   "source": [
    "pddls = []\n",
    "cur_state = problem.init\n",
    "for change in changes:\n",
    "    del_list = [c.replace(\"Deleting \", \"\") for c in change if \"Deleting\" in c]\n",
    "    add_list = [c.replace(\"Adding \", \"\") for c in change if \"Adding\" in c]\n",
    "    print(del_list)\n",
    "    print(add_list)\n",
    "    s = set()\n",
    "    for i in cur_state:\n",
    "        if str(i) not in del_list:\n",
    "            s.add(i)\n",
    "    for i in add_list:\n",
    "        s.add(Predicate(* i[1:-1].split(\" \")))\n",
    "    print(s)\n",
    "    p = Problem(name=problem.name, domain_name=problem.domain_name, requirements=problem.requirements, objects=problem.objects, init=s.copy(), goal=problem.goal)\n",
    "    pddls.append(problem_to_string(p))\n",
    "    cur_state = s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(holding a)'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(Predicate(\"holding\", \"a\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Deleting (on a c)', 'Deleting (clear a)', 'Deleting (handempty)', 'Adding (holding a)', 'Adding (clear c)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear c) (holding a) (on c d) (on d b) (ontable b))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (holding a)', 'Adding (clear a)', 'Adding (handempty)', 'Adding (ontable a)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear a) (clear c) (handempty) (on c d) (on d b) (ontable a) (ontable b))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (on c d)', 'Deleting (clear c)', 'Deleting (handempty)', 'Adding (holding c)', 'Adding (clear d)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear a) (clear d) (holding c) (on d b) (ontable a) (ontable b))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (clear a)', 'Deleting (holding c)', 'Adding (handempty)', 'Adding (clear c)', 'Adding (on c a)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear c) (clear d) (handempty) (on c a) (on d b) (ontable a) (ontable b))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (on d b)', 'Deleting (clear d)', 'Deleting (handempty)', 'Adding (holding d)', 'Adding (clear b)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear b) (clear c) (holding d) (on c a) (ontable a) (ontable b))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (holding d)', 'Adding (clear d)', 'Adding (handempty)', 'Adding (ontable d)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear b) (clear c) (clear d) (handempty) (on c a) (ontable a) (ontable b) (ontable d))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (on c a)', 'Deleting (clear c)', 'Deleting (handempty)', 'Adding (holding c)', 'Adding (clear a)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear a) (clear b) (clear d) (holding c) (ontable a) (ontable b) (ontable d))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (clear d)', 'Deleting (holding c)', 'Adding (handempty)', 'Adding (clear c)', 'Adding (on c d)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear a) (clear b) (clear c) (handempty) (on c d) (ontable a) (ontable b) (ontable d))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (clear b)', 'Deleting (ontable b)', 'Deleting (handempty)', 'Adding (holding b)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear a) (clear c) (holding b) (on c d) (ontable a) (ontable d))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n",
      "['Deleting (clear c)', 'Deleting (holding b)', 'Adding (handempty)', 'Adding (clear b)', 'Adding (on b c)']\n",
      "(define (problem BW-rand-4)\n",
      "    (:domain blocksworld-4ops)\n",
      "    (:objects a b c d)\n",
      "    (:init (clear a) (clear b) (handempty) (on b c) (on c d) (ontable a) (ontable d))\n",
      "    (:goal (and (on b c) (on c d)))\n",
      ")\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for c, p in zip(changes, pddls):\n",
    "    print(c)\n",
    "    print(p)\n",
    "    print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pddl.logic.predicates.Predicate'> (on a c)\n",
      "<class 'pddl.logic.predicates.Predicate'> (handempty)\n",
      "<class 'pddl.logic.predicates.Predicate'> (on c d)\n",
      "<class 'pddl.logic.predicates.Predicate'> (clear a)\n",
      "<class 'pddl.logic.predicates.Predicate'> (ontable b)\n",
      "<class 'pddl.logic.predicates.Predicate'> (on d b)\n"
     ]
    }
   ],
   "source": [
    "for i in problem.init:\n",
    "    print(type(i), i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "changes = \"\"\"Deleting (on a c)\\nDeleting (clear a)\\nDeleting (handempty)\\nAdding (holding a)\\nAdding (clear c)\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "del_list = [c.replace(\"Deleting \", \"\") for c in changes.split(\"\\n\") if \"Deleting\" in c]\n",
    "add_list = [c.replace(\"Adding \", \"\") for c in changes.split(\"\\n\") if \"Adding\" in c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "s = set()\n",
    "\n",
    "for i in problem.init:\n",
    "    if i not in del_list:\n",
    "        s.add(i)\n",
    "for i in add_list:\n",
    "    s.add(Predicate(* i[1:-1].split(\" \")))\n",
    "p = Problem(name=problem.name, domain_name=problem.domain_name, requirements=problem.requirements, objects=problem.objects, init=s, goal=problem.goal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "frozenset({Predicate(clear, a),\n",
       "           Predicate(handempty, ),\n",
       "           Predicate(on, a, c),\n",
       "           Predicate(on, c, d),\n",
       "           Predicate(on, d, b),\n",
       "           Predicate(ontable, b)})"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "problem.init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "frozenset({Predicate(clear, a),\n",
       "           Predicate(clear, c),\n",
       "           Predicate(handempty, ),\n",
       "           Predicate(holding, a),\n",
       "           Predicate(on, a, c),\n",
       "           Predicate(on, c, d),\n",
       "           Predicate(on, d, b),\n",
       "           Predicate(ontable, b)})"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.init"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Predicate(clear, a)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p.to"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'(clear a)'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Predicate(handempty, a, b)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Predicate(\"handempty\", \"a\", \"b\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "changes = \"\"\"Deleting (clear a)\\nDeleting (holding c)\\nAdding (handempty)\\nAdding (clear c)\\nAdding (on c a)\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "del_list = [c.replace(\"Deleting \", \"\") for c in changes.split(\"\\n\") if \"Deleting\" in c]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "default",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
